﻿using Hangout.Web.Core.Accounts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hangout.Web.Services.Account
{
    public class Account
    {

        Web.Model.HangoutDBEntities context = new Web.Model.HangoutDBEntities();

        public Services.Objects.UserData RegisterUser(string accessToken)
        {
            try
            {
                Core.Accounts.UserData? userdata =  Hangout.Web.Core.Accounts.User.RegisterUser(accessToken, ref context);
                if (userdata.HasValue)
                {
                    return new Objects.UserData { Age = userdata.Value.Profile.Age, Bio = userdata.Value.Profile.Bio, Birthday = userdata.Value.Profile.Birthday, DateTimeStamp = userdata.Value.Profile.DateTimeStamp, DateTimeUpdated = userdata.Value.Profile.DateTimeUpdated, DefaultLengthUnits = userdata.Value.Profile.DefaultLengthUnits, FirstName = userdata.Value.Profile.FirstName, Gender = userdata.Value.Profile.Gender, LargeProfilePicURL = userdata.Value.Profile.LargeProfilePicURL, LastName = userdata.Value.Profile.LastName, Link = userdata.Value.Profile.Link, Name = userdata.Value.Profile.Name, Phone = userdata.Value.Profile.Phone, ProfilePicURL = userdata.Value.Profile.ProfilePicURL, RelationshipStatus = userdata.Value.Profile.RelationshipStatus, Timezone = (float?)userdata.Value.Profile.Timezone, UserID = userdata.Value.User.UserID, FacebookAccessToken = userdata.Value.User.AccessToken, FacebookUserID = userdata.Value.User.FacebookUserID };
                }

                return null;
            }
            catch (System.Exception ex)
            {
                Web.Core.Exceptions.ExceptionReporting.AddAnException(0, Web.Core.ClientType.WindowsAzure, ex, ref context);
                return null;
            }
        }

        public  void UpdateUserData(Objects.UserData userData, string accessToken)
        {
            try
            {
                if (userData != null)
                {
                    if (Web.Core.Accounts.User.IsValid(userData.UserID, accessToken, context))
                    {
                        //create a user data

                        Model.UserProfile profile = new Model.UserProfile();

                        

                        profile.UserID = userData.UserID;
                        profile.Age = userData.Age;


                        profile.Bio = userData.Bio;
                        

                        profile.Birthday = userData.Birthday;
                        profile.DateTimeUpdated = DateTime.Now;
                        profile.DefaultLengthUnits = userData.DefaultLengthUnits;
                        profile.FirstName = userData.FirstName;
                        profile.Gender = userData.Gender;
                        profile.LargeProfilePicURL = userData.LargeProfilePicURL;
                        profile.LastName = userData.LastName;
                        profile.Link = userData.Link;
                        profile.Name = userData.Name;
                        profile.Phone = userData.Phone;
                        profile.ProfilePicURL = userData.ProfilePicURL;
                        profile.RelationshipStatus = userData.RelationshipStatus;
                        profile.Timezone = userData.Timezone;


                        Core.Accounts.UserData UserData = new Core.Accounts.UserData();
                        UserData.Profile = profile;
                        
                        



                        Hangout.Web.Core.Accounts.User.UpdateUserData(UserData, ref context);
                    }
                    else { throw new Web.Core.Exceptions.Account.UnAuthorisedAccess(); }
                }
            }
            catch (System.Exception ex)
            {
                Web.Core.Exceptions.ExceptionReporting.AddAnException(userData.UserID, Web.Core.ClientType.WindowsAzure, ex, ref context);
            }
        }


        public  Web.Services.Objects.UserProfile GetUserProfile(int userId, string accesstoken)
        {
            try
            {
                if (Web.Core.Accounts.User.IsValid(accesstoken, context))
                {
                    Web.Model.UserProfile profile= Hangout.Web.Core.Accounts.User.GetUserProfile(userId, context);
                    return new Objects.UserProfile{Age=(int)profile.Age,Gender=(bool)profile.Gender,RelationshipStatus=profile.RelationshipStatus,User=new Objects.User{Name=profile.Name,ProfilePicURL=profile.ProfilePicURL,Status=profile.Bio,UserID=profile.UserID}};

                }
                else { throw new Web.Core.Exceptions.Account.UnAuthorisedAccess(); }
            }
            catch (System.Exception ex)
            {
                Web.Core.Exceptions.ExceptionReporting.AddAnException(userId, Web.Core.ClientType.WindowsAzure, ex, ref context);
                return null;
            }
        }

    }
}